# CMakeLists.txt
cmake_minimum_required(VERSION 3.5.0)
project(led_project LANGUAGES ASM)

# enable_language 的形式不行，也可能是我的设置问题
# enable_language(ASM)
# 设置编译器和工具链
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_ASM_COMPILER arm-linux-gnueabihf-gcc)
set(CMAKE_C_COMPILER arm-linux-gnueabihf-gcc)
set(CMAKE_OBJCOPY arm-linux-gnueabihf-objcopy)
set(CMAKE_OBJDUMP arm-linux-gnueabihf-objdump)
# SET(CMAKE_LINKER "arm-linux-gnueabihf-ld")
# set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=arm-linux-gnueabihf-ld")
# set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -g")

# 添加源文件
# add_executable(led_elf led.s)

# set_target_properties(led_elf PROPERTIES LINK_FLAGS "-nostartfiles -nostdlib -Ttext 0X87800000")
message(${CMAKE_BINARY_DIR})

add_custom_command(
    OUTPUT hello
    COMMAND arm-linux-gnueabihf-gcc -g -c  ${PROJECT_SOURCE_DIR}/led.s -o ${CMAKE_BINARY_DIR}/led.o
)

add_custom_command(
    OUTPUT led_elf
    COMMAND arm-linux-gnueabihf-ld -Ttext 0X87800000 led.o -o led.elf
    DEPENDS hello
)

# 生成二进制文件
add_custom_command(
  OUTPUT led_bin
  COMMAND ${CMAKE_OBJCOPY} -O binary -S -g led.elf led.bin
  COMMENT "Generating binary file"
  DEPENDS led_elf
)

# 生成反汇编文件
add_custom_command(
  OUTPUT led_dis
  COMMAND ${CMAKE_OBJDUMP} -D led.elf > led.dis
  COMMENT "Generating disassembly file"
  DEPENDS led_bin
)

add_custom_target(hi ALL DEPENDS ${CMAKE_BINARY_DIR}/led_dis)

# 清理规则
function(clean)
  execute_process(COMMAND ${CMAKE_COMMAND} -E remove_directory_all ${CMAKE_CURRENT_BINARY_DIR})
  message(STATUS "Cleaning up...")
endfunction()
